require(['config'], function () {
    require(['jquery'], function () {
        require(['jqcolor', 'jqcookie'], function () {

            //数量的加减
            const goodnum = $('.optionnums #item_num');
            const sub = $('.optionnums .sub');
            const add = $('.optionnums .add');
            let num = 1;
            sub.click(function () {
                num--;
                if (num <= 0) {
                    num = 1;
                }
                goodnum.val(num);
            });
            add.click(function () {
                num++;
                if (num >= 150) {
                    num = 150;
                }
                goodnum.val(num);
            });
            goodnum.blur(function () {
                if (goodnum.val() == '') {
                    goodnum.val(1);
                } else if (goodnum.val() <= 0) {
                    goodnum.val(1);
                }
            });
            goodnum.focus(function () {
                goodnum.val('');
            })
            goodnum.change(function () {
                num = goodnum.val();
            })

            //选中类型和图片
            let goodlist = $('.main .inner .shopping-description .typesof .ajaxShowSkus .colortype .modnum');
            let fdjimg = $('.fdj img');
            goodlist.delegate('a', 'click', function () {
                $(this).css({ borderColor: '#08f', color: '#08f' }).siblings('a').css({ borderColor: '#e8e8e8', color: '#333333' });
            });
            let details = $('.main .inner .shopping-show .shopping-list ul');
            let bigpic = $('.main .shopping-big img')
            details.delegate('li', 'click', function () {
                $(this).css({ borderColor: '#000' }).siblings('li').css({ borderColor: '#F0F0F0', });
                bigpic.attr('src', $(this).children('img').attr('src'));
                fdjimg.attr('src', $(this).children('img').attr('src'));
            });
            // $.cookie('goodlist',num.val());

            //将数据存入cookie
            let sidarr = [];
            let dataarr = [];
            let numarr = [];
            let cartbtn = $('.shopping-description .typesof .staging .stagingpotion .staging-but #addToCart');
            let sid = location.search.substring(1).split('=')[1];
            let data = location.search.substring(1).split('=')[0];
            if ($.cookie('cookiesid') && $.cookie('cookienum') && $.cookie('cookiedata')) {
                sidarr = $.cookie('cookiesid').split(',');
                numarr = $.cookie('cookienum').split(',');
                dataarr = $.cookie('cookiedata').split(',');
            }

            cartbtn.click(function () {
                console.log(1);
                alert('商品添加成功');
                if (sidarr.indexOf(sid) !== -1) {
                    let index = sidarr.indexOf(sid);
                    numarr[index] = parseInt(numarr[index]) + parseInt(goodnum.val());
                    $.cookie('cookienum', numarr.toString(), { expires: 7 });
                } else {
                    sidarr.push(sid);
                    $.cookie('cookiesid', sidarr.toString(), { expires: 7 });
                    numarr.push(goodnum.val());
                    $.cookie('cookienum', numarr.toString(), { expires: 7 });
                    dataarr.push(data);
                    $.cookie('cookiedata', dataarr.toString(), { expires: 7 });
                }
            });

            //放大镜
            let wrap = $('.shopping-big');
            let microscope = $('.shopping-big .microscope');
            let fdj = $('.fdj')
            wrap.mouseover(function (ev) {
                microscope.show();
                fdj.show();
                wrap.mousemove(function (ev) {
                    var ev = ev || window.event;
                    sfmove(ev);
                });
            })
            wrap.mouseout(function () {
                microscope.hide();
                fdj.hide();
            });
            function sfmove(ev) {
                let l = ev.pageX - wrap.offset().left - microscope.width() / 2;
                let t = ev.pageY - wrap.offset().top - microscope.height() / 2;

                console.log(wrap.offset().top);
                // console.log(t);
                if (l < 0) {
                    l = 0;
                } else if (l >= bigpic.width() - microscope.width()) {
                    l = bigpic.width() - microscope.width() - 2;//2:边框的尺寸
                }

                if (t < 0) {
                    t = 0;
                } else if (t >= bigpic.height() - microscope.height()) {
                    t = bigpic.height() - microscope.height() - 2;
                }

                microscope.css({ left: l });
                microscope.css({ top: t });

                fdjimg.css({ left: -1.667 * l });
                fdjimg.css({ top: -1.667 * t });
            }
        });
    });
});